package com.ciis.base.utils;


import com.ciis.base.Constants;
import com.ciis.system.relation.domain.TSysRoleAuthority;
import com.ciis.system.user.domain.TSysUser;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 *
 * Session 工具类
 *
 */
public final class SessionUtils {
	
	protected static final Logger logger = Logger.getLogger(SessionUtils.class);

	/**
	  * 设置session的值
	  * @param request
	  * @param key
	  * @param value
	  */
	 public static void setAttr(HttpServletRequest request,String key,Object value){
		 request.getSession(true).setAttribute(key, value);
	 }
	 
	 
	 /**
	  * 获取session的值
	  * @param request
	  * @param key
	  */
	 public static Object getAttr(HttpServletRequest request,String key){
		 return request.getSession(true).getAttribute(key);
	 }
	 
	 /**
	  * 删除Session值
	  * @param request
	  * @param key
	  */
	 public static void removeAttr(HttpServletRequest request,String key){
		 request.getSession(true).removeAttribute(key);
	 }
	 
	 /**
	  * 设置用户信息 到session
	  * @param request
	  * @param user
	  */
	 public static void setUser(HttpServletRequest request,TSysUser user){
		 request.getSession(true).setAttribute(Constants.SESSION_USER, user);
	 }
	 
	 
	 /**
	  * 从session中获取用户信息
	  * @param request
	  * @return SysUser
	  */
	 public static TSysUser getUser(HttpServletRequest request){
		return (TSysUser)request.getSession(true).getAttribute(Constants.SESSION_USER);
	 }

    /**
     * 设置角色
     * @param request
     * @param roles
     */
     public static void setRole(HttpServletRequest request,HashMap<String,String> roles){
         request.getSession(true).setAttribute(Constants.SESSION_ROLE,roles);
     }

    /**
     * 设置权限
     * @param request
     * @param authMap
     */
     public static void setRights(HttpServletRequest request,HashMap<String,TSysRoleAuthority> authMap){
         request.getSession(true).setAttribute(Constants.SESSION_ROLE_RIGHTS,authMap);
     }

	 
	 /**
	  * 从session中获取用户信息
	  * @param request
	  * @return SysUser
	  */
	 public static void removeUser(HttpServletRequest request){
		removeAttr(request, Constants.SESSION_USER);
        removeAttr(request, Constants.SESSION_ROLE);
        removeAttr(request,Constants.SESSION_ROLE_RIGHTS);
	 }
	 
	 
	 /**
	  * 设置验证码 到session
	  * @param request
	  */
	 public static void setValidateCode(HttpServletRequest request,String validateCode){
		 request.getSession(true).setAttribute(Constants.SESSION_SECURITY_CODE, validateCode);
	 }
	 
	 
	 /**
	  * 从session中获取验证码
	  * @param request
	  * @return SysUser
	  */
	 public static String getValidateCode(HttpServletRequest request){
		return (String)request.getSession(true).getAttribute(Constants.SESSION_SECURITY_CODE);
	 }
	 
	 
	 /**
	  * 从session中获删除验证码
	  * @param request
	  * @return SysUser
	  */
	 public static void removeValidateCode(HttpServletRequest request){
		removeAttr(request, Constants.SESSION_SECURITY_CODE);
	 }
	 
	 /**
	  * 判断当前登录用户是否超级管理员
	  * @param request
	  * @return
	  */
	 public static boolean isAdmin(HttpServletRequest request){ //判断登录用户是否超级管理员
		 TSysUser user =  getUser(request);
		 if(user == null  || user.getSuperAdmin() != Constants.SUPERADMIN_YES){
			 return false;
		 }
		 return true;
	 }
	 
	 
	 
	 /**
	  * 判断当前登录用户是否超级管理员
	  * @param request
	  * @return
	  */
	 public static void setAccessUrl(HttpServletRequest request,List<String> accessUrls){ //判断登录用户是否超级管理员
		 setAttr(request, Constants.SESSION_ACCESS_URLS, accessUrls);
	 }
	 
	 
	 
	 /**
	  * 判断URL是否可访问
	  * @param request
	  * @return
	  */
	 public static boolean isAccessUrl(HttpServletRequest request,String url){ 
		 List<String> accessUrls = (List)getAttr(request, Constants.SESSION_ACCESS_URLS);
		 if(accessUrls == null ||accessUrls.isEmpty() || !accessUrls.contains(url)){
			 return false;
		 }
		 return true;
	 }
	 

}